package org.zxyj.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.zxyj.core.constants.Keys;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.entity.Result;
import org.zxyj.core.web.controller.BaseController;
import org.zxyj.manage.entity.CoursePaperCrop;
import org.zxyj.manage.service.ICoursePaperService;
import org.zxyj.manage.service.ICourseTemplateService;
import org.zxyj.manage.service.IMarkingPaperService;

/**
 * 客观题阅卷管理控制类
    * @ClassName: MarkingPaperObjectiveController
    * @author liuyong
    * @date 2016年8月26日
 */
@Controller
@RequestMapping("/manage/markingPaper/objective")
public class MarkingPaperObjectiveController extends BaseController{
    @Autowired
	private ICourseTemplateService courseTemplateService;
    @Autowired
    private IMarkingPaperService markingPaperService;
    @Autowired
    private ICoursePaperService coursePaperService;
    
    @RequestMapping({"","/","/index"})
	public ModelAndView index(ModelAndView mv) {
		mv.setViewName(Views.LIST_VIEW);
		Map<String, Object> criteria = new HashMap<String, Object>();
		//查询可阅批次答卷
		String batchId = getParaValue("batchId");
		String gradeNo = getParaValue("gradeNo");
		String courseNo = getParaValue("courseNo");
        criteria.put("batchId",batchId);
        criteria.put("gradeNo",gradeNo);
        criteria.put("courseNo",courseNo);
		Pagination page = (Pagination) getRequestAttr("pagination");
		Pagination pagination = this.markingPaperService.findMarkingPaperObjectivePage(criteria, page);
		mv.addObject(Keys.KEY_PAGINATION, pagination);
		mv.addObject("batchs", courseTemplateService.getExamBatchs(criteria));
		mv.addObject("batchId", batchId);
		mv.addObject("gradeNo", gradeNo);
		mv.addObject("courseNo", courseNo);
		return mv;
    }
    
    @RequestMapping("/viewObjectiveStudentUI")
    public ModelAndView viewObjectiveStudentUI(ModelAndView mv){
    	mv.setViewName(Views.STU_SCORE_VIEW);
    	Map<String, Object> criteria = new HashMap<String, Object>();
    	String tempId = getParaValue("tempId");
    	criteria.put("tempId", tempId);
    	String bjbh = getParaValue("bjbh");
        criteria.put("bjbh",bjbh);
        String khxm = getParaValue("khxm");
        criteria.put("khxm",khxm);
    	Pagination page = (Pagination) getRequestAttr("pagination");
		Pagination pagination = this.markingPaperService.findMarkingPaperObjectiveStudentPage(criteria, page);
		mv.addObject(Keys.KEY_PAGINATION, pagination);
		mv.addObject("tempId", tempId);
		mv.addObject("bjbh", bjbh);
		mv.addObject("khxm", khxm);
    	return mv;
    }
    
    /**
     * 客观题阅卷处理进度
        * @Title: getHandleSession
        * @param @return    参数
        * @return Result<?>    返回类型
        * @throws
     */
    @ResponseBody
    @RequestMapping("/getHandleSession")
    public Result<?> getHandleSession(){
    	Result<?> result = new Result<String>();
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	int djzs = getSessionAttr("djzs_objective")==null?0:Integer.parseInt(getSessionAttr("djzs_objective")+"");
        int clzs = getSessionAttr("clzs_objective")==null?0:Integer.parseInt(getSessionAttr("clzs_objective")+"");
        int succCount = getSessionAttr("succCount_objective")==null?0:Integer.parseInt(getSessionAttr("succCount_objective")+"");
        int failCount = getSessionAttr("failCount_objective")==null?0:Integer.parseInt(getSessionAttr("failCount_objective")+"");
        int bl = djzs > 0 && clzs > 0 ? (clzs / djzs * 100) : 0;
		resultMap.put("djzs", djzs);
		resultMap.put("clzs", clzs);
		resultMap.put("bl", bl);
		resultMap.put("succCount", succCount);
		resultMap.put("failCount", failCount);
    	result.setData(resultMap);
		result.setResultCode(getSessionAttr("resultCode_objective")+"");
    	return result;
    }
    
    /**
     * 执行阅卷任务处理-客观题分数计算
        * @Title: markingHandle
        * @param     参数
        * @return void    返回类型
        * @throws
     */
    @ResponseBody
    @RequestMapping("/markingHandle/exec")
    public void markingHandle(@RequestBody Map<String,Object> criteria,HttpSession session){
    	//清空session
    	removeSessionAttr("djzs_objective");
    	removeSessionAttr("clzs_objective");
    	removeSessionAttr("succCount_objective");
    	removeSessionAttr("failCount_objective");
    	removeSessionAttr("resultCode_objective");
    	
    	criteria.put("session", session);
    	this.markingPaperService.objectiveAnswerRecognition(criteria);
    }
    
    @RequestMapping("/viewObjectiveScoreDetailUI")
    public ModelAndView viewObjectiveScoreDetailUI(ModelAndView mv){
    	mv.setViewName(Views.SCORE_VIEW);
    	Map<String, Object> criteria = new HashMap<String, Object>();
    	criteria.put("tempId", getParaValue("tempId"));
    	criteria.put("stuNo", getParaValue("stuNo"));
    	mv.addObject(Keys.KEY_PARAM, this.markingPaperService.findMarkingPaperObjectiveStudentScore(criteria));
    	return mv;
    }
    
    @RequestMapping("/viewObjectiveImageUI")
    public ModelAndView viewObjectiveImageUI(ModelAndView mv){
    	mv.setViewName(Views.OBJ_IMG_VIEW);
    	Map<String, Object> criteria = new HashMap<String, Object>();
    	criteria.put("courseTemplateId", getParaValue("tempId"));
    	criteria.put("studentNo", getParaValue("stuNo"));
    	criteria.put("type", -1);
    	String path = "";
    	CoursePaperCrop entity = this.coursePaperService.findCoursePaperCropEntity(criteria);
    	if(null != entity){
    		path = entity.getFilePath();
    	}
    	mv.addObject("path", path);
    	return mv;
    }
    
    class Views {
		/**
		 * 列表视图
		 */
		public static final String LIST_VIEW = "/manage/markingPaper/objective_list";
		
		/**
		 * 客观题得分视图
		 */
		public static final String STU_SCORE_VIEW = "/manage/markingPaper/objective_student";
		
		/**
		 * 学生客观题具体得分视图
		 */
		public static final String SCORE_VIEW = "/manage/markingPaper/objective_student_score";
		
		/**
		 * 学生客观题原图视图
		 */
		public static final String OBJ_IMG_VIEW = "/manage/markingPaper/objective_student_image";
    }
}
